package com.lanou.dllo.vc;

import android.app.Application;
import android.content.Context;
import android.content.IntentFilter;

import com.baidu.mapapi.SDKInitializer;
import com.easemob.EMCallBack;
import com.easemob.chat.EMChat;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMChatOptions;
import com.lanou.dllo.vc.receiver.CallReceiver;
import com.lanou.dllo.vc.volley.DemoHXSDKHelper;

/**
 * Created by dllo on 15/11/24.
 */
public  class MyApplication extends Application {
    public static Context applicationContext;
    private static MyApplication instance;
    public final  String PREF_USERNAME = "username";
    /*
    * 当前用户nickname,为了苹果推送不是userid二十昵称*/
    public static String currentUserNick = "";

    public static DemoHXSDKHelper hxsdkHelper = new DemoHXSDKHelper();
    private CallReceiver callReceiver;


    @Override
    public void onCreate() {
        super.onCreate();
        applicationContext = this;
        instance = this;
        // EMChat.getInstance().init(applicationContext);
        hxsdkHelper.onInit(applicationContext);

        // 获取到EMChatOptions对象
        EMChatOptions options = EMChatManager.getInstance().getChatOptions();
        // 默认添加好友时，是不需要验证的，改成需要验证
        options.setAcceptInvitationAlways(false);
        // 默认环信是不维护好友关系列表的，如果app依赖环信的好友关系，把这个属性设置为true
        options.setUseRoster(true);
        EMChat.getInstance().setAppInited();

        //百度地图初始化
        SDKInitializer.initialize(this);//百度地图初始化工作
        //注册实时通话监听
        IntentFilter callFilter = new IntentFilter(EMChatManager.getInstance().getIncomingCallBroadcastAction());
        registerReceiver(new CallReceiver(), callFilter);
        if(callReceiver == null){
            callReceiver = new CallReceiver();
        }
        //注册通话广播接收者
        this.registerReceiver(callReceiver, callFilter);

//        Parse.enableLocalDatastore(this);
//
//        Parse.initialize(this, "mrvEWxrRPyUAIGUm5D29d1vW3PpqgQFkVyr1pFDR", "iRFuaouQDanDKQX8Em0bAu7SexY7jqIPN0bBgAMP");
        

    }

    public static Context getContext(){
        return applicationContext;
    }

    public static MyApplication getInstance(){
        return  instance;
    }

    //获取当前登录用户名
    public static String getUserName(){
        return hxsdkHelper.getHXId();
    }

    /**
     * 获取当前登陆用户名
     *
     * @return
     */
    public void setUserName(String username) {
        hxsdkHelper.setHXId(username);
    }
    //获取密码
    public String getPassword(){
        return hxsdkHelper.getPassword();
    }

    /**
     * 设置密码 下面的实例代码 只是demo，实际的应用中需要加password 加密后存入 preference 环信sdk
     * 内部的自动登录需要的密码，已经加密存储了
     *
     * @param pwd
     */

    public void setPassword(String pwd){
        hxsdkHelper.setPassword(pwd);
    }

    //退出登录,清空数据

    public void logout(final  boolean isGCM, final EMCallBack emCallBack){
        hxsdkHelper.logout(isGCM,emCallBack);
    }


}